home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc / Developer Documentation / Recipes, Tech Notes & Articles / Recipes / Data Interchange / Promising non-OpenDoc file < prev   
Encoding:
Text File  |  1995-11-08  |  3.0 KB  |  90 lines  |  [TEXT/ttxt]

  1. OpenDocâ„¢ Recipes
  2.  
  3.  
  4. Drag-and-Drop Recipes -
  5. Promising non-OpenDoc file
  6. By The OpenDoc Design Team
  7. November 8, 1995
  8.  
  9. © 1993-1995  Apple Computer, Inc. All Rights Reserved.
  10. Apple, the Apple logo, AppleScript, Bento, Macintosh, QuickTime, and OpenDoc are 
  11. registered trademarks of Apple Computer, Inc.
  12. Finder, Mac, and QuickDraw are trademarks of Apple Computer, Inc. 
  13. SOM, SOMObjects, and System Object Model are licensed trademarks of IBM Corporation. 
  14.  
  15.  
  16. Promising a non-OpenDoc file 
  17.  
  18. The Drag and Drop mechanism allows the source part to create a non-OpenDoc file when the destination is the Finder or other applications which can accept a file.
  19.  
  20. Recipes
  21.  
  22. Initiating a drag:
  23.  
  24. If the source part puts a promise value of kODHFSPromise type in the contents property of the content storage unit of the ODDragAndDrop object, the Drag and Drop mechanism would ask the source part to fulfill the promise when a drop happens in the Finder or other applications which can accept a file.
  25.  
  26. The following is the code fragment for creating such a promise. This piece of code is called right before ODDragAndDrop::StartDrag.
  27.  
  28. PromiseHFSFlavor    thePromise;
  29. thePromise.fileType = 'TEXT';
  30. thePromise.fileCreator = 'ttxt';
  31. thePromise.fdFlags = 0;
  32. dadsu->Focus(ev, kODPropContents, kODPosUndefined, kODNULL, 0, kODPosUndefined);
  33. StorageUnitSetPromiseValue(dadsu, ev, kODHFSPromise, 0, sizeof(PromiseHFSFlavor), &thePromise,_fPartWrapper);
  34.  
  35.  
  36. Fulfilling the promise / creating the non-OpenDoc file:
  37.  
  38. In your part editor's FulfillPromise, you should handle the case when the promise value needs to be fulfilled. The following is a code fragment for creating a text file.
  39.  
  40.     ODValueType valueType = promiseSUView->GetType(ev);
  41.     if (ODISOStrEqual(valueType, kODHFSPromise))
  42.     {
  43.         ODByteArray    ba;
  44.         short fileRefNum = 0;
  45.         
  46.         ODVolatile(ba);
  47.         ODVolatile(fileRefNum);
  48.         
  49.         SOM_TRY
  50.             const Str63 fileName = "\psome text file";    // hard-coded for illustration.
  51.             FSSpec dropFSSpec;
  52.             StorageUnitViewGetValue(promiseSUView, ev, sizeof(FSSpec), &dropFSSpec);
  53.             ODBlockMove(fileName, &(dropFSSpec.name), fileName[0]+ 1);
  54.             promiseSUView->SetOffset(ev, 0);
  55.             
  56.             ba._buffer = kODNULL;
  57.             ODStorageUnit* su = promiseSUView->GetStorageUnit(ev);
  58.             su->Focus(ev, kODPropContents, kODPosUndefined, kODAppleTEXT, 0, kODPosUndefined);
  59.             long size = su->GetSize(ev);
  60.             size = su->GetValue(ev, size, &ba);
  61.             
  62.             OSErr err;
  63.             if (((err = FSpCreate(&dropFSSpec, 'ttxt','TEXT', smSystemScript)) == noErr) &&
  64.                 ((err = FSpOpenDF(&dropFSSpec, fsRdWrPerm, &fileRefNum)) == noErr) &&
  65.                 ((err = FSWrite(fileRefNum, &size, ba._buffer)) == noErr) &&
  66.                 ((err = FSClose(fileRefNum)) == noErr))
  67.             {
  68.                 StorageUnitViewSetValue(promiseSUView, ev, sizeof(FSSpec), &dropFSSpec);
  69.             }
  70.             else
  71.             {
  72.                 ODULong size = promiseSUView->GetSize(ev);
  73.                 promiseSUView->DeleteValue(ev, size);
  74.             }
  75.             ODDisposePtr(ba._buffer);
  76.             
  77.  
  78.         SOM_CATCH_ALL
  79.         
  80.             if (fileRefNum)
  81.                 FSClose(fileRefNum);
  82.             
  83.             ODDisposePtr(ba._buffer);
  84.             
  85.             ODSetSOMException(ev, 0);
  86.             
  87.         SOM_ENDTRY
  88.     }
  89.     ODDisposePtr(valueType);
  90.